﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using BaBaoFan.Modifier.Model;
using BaBaoFan.Modifier.Models;
using BaBaoFan.Modifier.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;

namespace BaBaoFan.Modifier.Controllers
{
    public class NavigationRelationController : BaseController
    {
        private readonly INavigationRelationService navigationRelationService;
        private readonly INavigationService navigationService;
        private readonly IMapper mapper;
        public NavigationRelationController(INavigationRelationService navigationRelationService, INavigationService navigationService, IMapper mapper, IMemoryCache memoryCache) : base(memoryCache)
        {

            this.navigationRelationService = navigationRelationService;
            this.navigationService = navigationService;
            this.mapper = mapper;
        }
        public IActionResult Index()
        {
            //var navigationRelations = navigationRelationService.GetNavigationRelations();
            var navigationRelations = new List<NavigationRelation>();
            return View(navigationRelations);
        }

        public IActionResult Delete(int[] ids)
        {
            navigationRelationService.Delete(ids);
            return Json("ok");
        }
        [HttpGet]
        public IActionResult Add()
        {
            var vm = new NavigationRelationCreateOrUpdateViewModel();
            vm.AvaliableClasses = navigationService.GetAllNavigationColumn().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            vm.AvaliableNavigationInfos = navigationService.GetAllNavigationInfo().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            return View(vm);
        }
        [HttpPost]
        public IActionResult Add(NavigationRelationCreateOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
                return View(vm);
            var model = mapper.Map<NavigationRelationEntity>(vm);
            navigationRelationService.Add(model);
            return Redirect(nameof(Index));
        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            var model = navigationRelationService.GetById(id);
            var vm = mapper.Map<NavigationRelationCreateOrUpdateViewModel>(model);
            vm.AvaliableClasses = navigationService.GetAllNavigationColumn().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            vm.AvaliableNavigationInfos = navigationService.GetAllNavigationInfo().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            return View(vm);
        }
        [HttpPost]
        public IActionResult Update(NavigationRelationCreateOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
                return View(vm);
            var model = mapper.Map<NavigationRelationEntity>(vm);
            navigationRelationService.Update(model);
            return Redirect(nameof(Index));
        }
        [HttpPost]
        public IActionResult Search(string navigationInfoName, string className, string columnId, int page = 1, int limit = 10)
        {
            var navigationCondition = new RelationCondition
            {
                NavigationInfoName = navigationInfoName,
                ClassName = className,
                ColumnId = columnId,
                PageIndex = page,
                PageSize = limit,
                TotalCount = 0
            };
            var navigationRelations = navigationRelationService.Search(navigationCondition);
            return Json(new { code = 0, count = navigationCondition.TotalCount, msg = "", data = navigationRelations });
        }
    }
}